package com.yuqiangz.boot05web01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 赵雨强
 * @create 2021/7/24 22:23
 * @description 测试@RequestAttribute
 */

@Controller
public class RequestController {

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        request.setAttribute("msg", "成功了。。。");
        request.setAttribute("code", 200);
        return "forward:/success";
    }

    @ResponseBody
    @GetMapping("/success")
    public String success(@RequestAttribute("msg") String msg,
                          @RequestAttribute("code") Integer code,
                          HttpServletRequest request){

        Object msg1 = request.getAttribute("msg");
        System.out.println(msg1);
        Object code1 = request.getAttribute("code");
        System.out.println(code1);
        String s1 = msg + code;
        return s1;
    }


    @GetMapping("/params")
    public String testParam(Map<String, String> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response){

        map.put("hello", "世界");
        model.addAttribute("world","儋州");
        request.setAttribute("message", "请求域");

        Cookie cookie = new Cookie("c1", "v1");
        cookie.setDomain("localhost");
        response.addCookie(cookie);
        return "forward:/success01";
    }


    @ResponseBody
    @GetMapping("/success01")
    public String success01(@RequestAttribute("hello") String msg,
                          @RequestAttribute("world") String code,
                          HttpServletRequest request){


        Object msg2 = request.getAttribute("hello");
        System.out.println(msg2);
        Object msg3 = request.getAttribute("world");
        System.out.println(msg3);
        Object msg4 = request.getAttribute("message");
        System.out.println(msg4);
        String s1 = msg + code;
        return s1;
    }
}
